package com.springboot.chongwu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.chongwu.entity.SysRoleMenu;
import com.springboot.chongwu.mapper.SysRoleMenuMapper;
import com.springboot.chongwu.service.ISysRoleMenuService;
import com.springboot.chongwu.util.IdentityUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单ID 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-06-17
 */
@Service
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements ISysRoleMenuService {

    /**
     * 根据当前角色获取当前菜单ID
     * @param roleId
     * @return
     */
    @Override
    public List<Object> getByRoleId(String roleId) {
        QueryWrapper<SysRoleMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        queryWrapper.select("menu_id");
        List<Object> hjRoleMenus=this.listObjs(queryWrapper);
        return hjRoleMenus;
    }

    /**
     * 根据角色删除当前关联的菜单角色
     * @param roleId
     * @return
     */
    @Override
    public boolean removeMenuRoleId(String roleId) {
        QueryWrapper<SysRoleMenu> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        return this.remove(queryWrapper);
    }

    /**
     * 新增菜单角色关联表
     * @param menuIds 菜单集合
     * @param roleId 角色ID
     * @return
     */
    @Override
    public boolean saveRoleMenu(String[] menuIds,String roleId) {
        //新增权限角色关联表
        List<SysRoleMenu> hjRoleMenus=null;
        if(menuIds!=null&&menuIds.length>=1){
            hjRoleMenus=new ArrayList<>();
            SysRoleMenu hjRoleMenu=null;
            for (String menuId :menuIds) {
                hjRoleMenu=new SysRoleMenu();
                hjRoleMenu.setRoleMenuId(IdentityUtil.identityId("RM"));
                hjRoleMenu.setMenuId(menuId);
                hjRoleMenu.setRoleId(roleId);
                hjRoleMenus.add(hjRoleMenu);
            }
            return this.saveBatch(hjRoleMenus);
        }
        return true;
    }

    /**
     * 获取当前账户权限码集合
     * @param loginId
     * @return
     */
    @Override
    public List<String> getMenuList(Object loginId) {
        return this.baseMapper.getMenuList(loginId);
    }
}
